﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel.Composition;
using System.Data;
using System.Linq;
using System.Text;
using Caliburn.Core.InversionOfControl;
using Caliburn.PresentationFramework;
using Caliburn.PresentationFramework.Filters;
using Caliburn.PresentationFramework.Invocation;
using Caliburn.PresentationFramework.RoutedMessaging;
using Caliburn.ShellFramework.Results;
using MTGenome.Framework;
using MTGenome.MTG;

namespace MTGenome.ViewModels
{
    [Singleton(typeof(CardCollectionViewModel)), PartCreationPolicy(CreationPolicy.Shared)]
    public sealed class CardCollectionViewModel : RescueBase
    {
        private readonly DeckViewModel _deck = null;
        private readonly CardDetailViewModel _cardDetail = null;
        private object _lock = new object();
        private bool _loading = false;

        public CardCollectionViewModel(DeckViewModel deck, CardDetailViewModel cardDetail)
        {
            _deck = deck;
            _cardDetail = cardDetail;

            Cards = new BindableCollection<ICard>();
        }

        public ICard SelectedCard { get; set; }

        public BindableCollection<ICard> Cards
        {
            get;
            set;
        }

        public void Loaded()
        {
            if (!_loading)
            {
                Execute.OnBackgroundThread(() =>
                {
                    lock (_lock)
                    {
                        _loading = true;
                        Cards.Clear();

                        using (IDatabase database = IoC.Get<IDatabase>())
                        {
                            var query = database.Cards.OrderBy(c => c.Name);

                            Cards.AddRange(query);
                        }
                        _loading = false;
                    }
                });
            }
        }

        public IEnumerable<IResult> CardSelected()
        {
            yield return new AsyncResult(() =>
            {
                _cardDetail.Card = SelectedCard;
            });
        }

        public IEnumerable<IResult> AddOneCardToDeck()
        {
            yield return new AsyncResult(() =>
            {
                _deck.AddCardsToDeck(1);
            });
        }

        public IEnumerable<IResult> AddFourCardsToDeck()
        {
            yield return new AsyncResult(() =>
            {
                _deck.AddCardsToDeck(4);
            });
        }

        public IEnumerable<IResult> AddOneCardToSideBoard()
        {
            yield return new AsyncResult(() =>
            {
                _deck.AddCardsToSideboard(1);
            });
        }

        public IEnumerable<IResult> AddFourCardsToSideBoard()
        {
            yield return new AsyncResult(() =>
            {
                _deck.AddCardsToSideboard(4);
            });
        }
    }
}
